const winston = require('../utils/winstonLogger.js');
const orderInfoModel = require('../models/orderInfo.js');
const foodModel = require('../models/Food.js');
const customerModel = require('../models/Customer.js');
var mongoose = require('mongoose');
//connect to mongodb
mongoose.set('debug',true);
mongoose.connect(
    'mongodb://localhost:27017/test',
    {useMongoClient:true}
).then(function(db){
    winston.info('DB connect success !!')
}).catch(function(err){
    winston.error('DB connect failed!!');
    winston.error(err.stack);
});
// module.exports = {
//     addOrderInfo : addOrderInfo,
//     updateOrderInfo : updateOrderInfo,
//     deleteOrderInfo : deleteOrderInfo,
//     queryorderInfo : queryorderInfo
// }
var orderInfoDao ={};
orderInfoDao.addOrderInfo =function (orderInfos,callback) {
    orderInfoModel.insertMany(orderInfos,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

orderInfoDao.deleterderInfo =function (orderInfoId,callback) {
    orderInfoModel.deleteMany({_id:orderInfoId},function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

orderInfoDao.updateorderInfo =function (neworderInfo,callback) {
    orderInfoModel.updateMany({_id:neworderInfo._id},neworderInfo,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

orderInfoDao.queryOrderInfo  =function ($n={},callback){
    orderInfoModel.find($n,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    }).populate('customer')
        .populate('foods.food');
}
var date = new Date();
var orderInfos =[{
    "customer":"59ac156ad8c06b85c833cd67",
    "merchant":"59aba01c83009751b4914696",
    "foods":[{
        food:"59abb8106e5498850cae668e",
        num:3,
        "subTotalMoney":3433
    },{
        food:"59abbd7853b78077ac422e85",
        num:3
    },{
        food:"59a8f569a39f92222cbf6827",
        num:71,
        "subTotalMoney":3433
    }],
    "address": "海怡湾畔",
    "phone": "123123412",
    "status": "等待商家接单",
    "allTotalMoney": 111,  //总价钱
    "createDate": date,
},{
    "customer":"59ac156ad8c06b85c833cd67",
    "merchant":"59aba01c83009751b4914696",
    "foods":[{
        food:"59a8f569a39f92222cbf6826",
        num:2,
        "subTotalMoney":3.7
    },{
        food:"59abbd7853b78077ac422e85",
        num:1
    },{
        food:"59a8f569a39f92222cbf6827",
        num:3,
        "subTotalMoney":3.4
    }],
    "address": "软件园路",
    "phone": "123456789",
    "status": "等待商家接单",
    "allTotalMoney": 222,  //总价钱
    "createDate": date,
},{
    "merchant":"59aba01c83009751b4914696",
    "customer":"59ac1558d8c06b85c833cd65",
    "foods":[{"food":"59abb8106e5498850cae668e","num":6,"subTotalMoney":32},
        {"food":"59abbd7853b78077ac422e85","num":2,"subTotalMoney":3.4}],
    "address": "海怡湾畔",
    "phone": "1111111",
    "status": "等待商家接单",
    "allTotalMoney": 555,  //总价钱
    "createDate": date,  //订单日期
},{
    "merchant":"59aba01c83009751b4914696",
    "customer":"59ac1558d8c06b85c833cd65",
    "foods":[{"food":"59abb8106e5498850cae668e","num":6,"subTotalMoney":32},
        {"food":"59abbd7853b78077ac422e85","num":2,"subTotalMoney":3.4}],
    "address": "海怡湾畔",
    "phone": "1111111",
    "status": "等待商家接单",
    "allTotalMoney": 444,  //总价钱
    "createDate": date,  //订单日期
},{
    "merchant":"59aba01c83009751b4914696",
    "customer":"59ac1558d8c06b85c833cd65",
    "foods":[{"food":"59abb8106e5498850cae668e","num":6,"subTotalMoney":32},
        {"food":"59abbd7853b78077ac422e85","num":2,"subTotalMoney":3.4}],
    "address": "海怡湾畔",
    "phone": "1111111",
    "status": "等待商家接单",
    "allTotalMoney": 454,  //总价钱
    "createDate": date,  //订单日期
}]
var callback = function (err,result) {
    if(err){
        console.log(err);
    }else{
        console.log(JSON.stringify(result));

    }
}

var newOrderInfo = {
    _id: "59a9282ec173042174fdba54",
    "foods":[{"food":"59a8f2287c07a80134efd769","num":3},{"food":"59a8f2287c07a80134efd767","num":3}],
    "address": "软件园路",
    "phone": "55555555",
    "status": "receive"
}

orderInfoDao.addOrderInfo(orderInfos,callback);
//orderInfoDao.updateorderInfo(newOrderInfo,callback);
//orderInfoDao.queryOrderInfo({merchant:"59aba01c83009751b4914696"},callback);
//orderInfoDao.deleterderInfo("59a9442c1fb4b8263461996c",callback)
